
## load data
contsurv <- read.csv("data/lucid experimental with control condition data.csv", colClasses = "character")[c(-1,-2),]

## some respondents did not take the survey after reading consent
contsurv <- contsurv[contsurv$Q1 == "Yes",]

# some respondents never finished
drops <- contsurv$T1Q1 == "" & contsurv$T2Q1 == "" & contsurv$CQ1 == "" & contsurv$T1Q2 == "" & contsurv$T2Q2 == "" & contsurv$CQ2 == ""
contsurv <- contsurv[drops == FALSE,]

## rename questions
names(contsurv)
names(contsurv)[c(
   4:9,
   2:3,
   16:18,
   19:21)] <- 
  c("thrm_cong","thrm_corp","thrm_ent","thrm_cath","thrm_who","thrm_nfl",
   "ideology","party",
   "med_job","med_inq","med_pol",
   "employment","emp_size","emp_size_unemp")

## reassign answers for repeated questions to final answer

# treatment indicator variable
contsurv$treatment <- "1"
contsurv$treatment[contsurv$T1Q1 == "" & contsurv$CQ1 == ""] <- "2"
contsurv$treatment[contsurv$T1Q1 == "" & contsurv$T2Q1 == ""] <- "3"

# support for trade and trade agreements
contsurv$TradeOpenFavor <- NA
contsurv$TradeOpenFavor[contsurv$treatment == "1"] <- contsurv$T1Q1[contsurv$treatment == "1"]
contsurv$TradeOpenFavor[contsurv$treatment == "2"] <- contsurv$T2Q1[contsurv$treatment == "2"]
contsurv$TradeOpenFavor[contsurv$treatment == "3"] <- contsurv$CQ1[contsurv$treatment == "3"]

contsurv$TradeDealFavor <- NA
contsurv$TradeDealFavor[contsurv$treatment == "1"] <- contsurv$T1Q2[contsurv$treatment == "1"]
contsurv$TradeDealFavor[contsurv$treatment == "2"] <- contsurv$T2Q2[contsurv$treatment == "2"]
contsurv$TradeDealFavor[contsurv$treatment == "3"] <- contsurv$CQ2[contsurv$treatment == "3"]

# employer size
contsurv$emp_size_comb <- contsurv$emp_size; contsurv$emp_size_comb[contsurv$emp_size_comb == ""] <- contsurv$emp_size_unemp[contsurv$emp_size_comb == ""]

# race
race2 <- rep("Other non-white", nrow(contsurv))
race2[contsurv$ethnicity == 1] <- "White"
race2[contsurv$ethnicity == 2] <- "Black"
race2[contsurv$hispanic %in% c(1,15) == FALSE] <- "Latino"
race2[contsurv$hispanic %in% c(3:14)] <- "AAPI"
contsurv$race <- factor(race2, levels = c("White","Black","Latino","AAPI","Other non-white"))

# gender
gender2 <- rep("Female", nrow(contsurv))
gender2[contsurv$gender == 1] <- "Male"
contsurv$gender <- factor(gender2, levels = c("Female","Male"))

## create numeric versions of variables
# support for trade and trade agreements
contsurv$TradeOpenFavorNum <- NA; contsurv$TradeOpenFavorNum[contsurv$TradeOpenFavor == "Oppose a great deal"] <- 1
contsurv$TradeOpenFavorNum[contsurv$TradeOpenFavor == "Oppose somewhat"] <- 2; contsurv$TradeOpenFavorNum[contsurv$TradeOpenFavor == "Neither favor nor oppose"] <- 3
contsurv$TradeOpenFavorNum[contsurv$TradeOpenFavor == "Favor somewhat"] <- 4; contsurv$TradeOpenFavorNum[contsurv$TradeOpenFavor == "Favor a great deal"] <- 5

contsurv$TradeDealFavorNum <- NA; contsurv$TradeDealFavorNum[contsurv$TradeDealFavor == "Oppose a great deal"] <- 1
contsurv$TradeDealFavorNum[contsurv$TradeDealFavor == "Oppose somewhat"] <- 2; contsurv$TradeDealFavorNum[contsurv$TradeDealFavor == "Neither favor nor oppose"] <- 3
contsurv$TradeDealFavorNum[contsurv$TradeDealFavor == "Favor somewhat"] <- 4; contsurv$TradeDealFavorNum[contsurv$TradeDealFavor == "Favor a great deal"] <- 5

# mediators convert to numeric.
contsurv$med_job <- factor(contsurv$med_job, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
contsurv$med_job <- as.numeric(contsurv$med_job)
contsurv$med_inq <- factor(contsurv$med_inq, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
contsurv$med_inq <- as.numeric(contsurv$med_inq)
contsurv$med_pol <- factor(contsurv$med_pol, levels = c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))
contsurv$med_pol <- as.numeric(contsurv$med_pol)

# party and ideology, turn to numeric scales
contsurv$party <- factor(contsurv$party, levels = c("Strong Democrat","Weak Democrat","Independent - leaning Democrat",
  "Independent - no preference","Independent - leaning Republican","Weak Republican","Strong Republican"))
contsurv$party <- as.numeric(contsurv$party)
contsurv$ideology <- factor(contsurv$ideology, levels = c("Extremely liberal","Liberal","Slightly liberal","Moderate; middle of the road",
  "Slightly conservative","Conservative","Extremely conservative"))
contsurv$ideology <- as.numeric(contsurv$ideology)
contsurv$income <- as.numeric(contsurv$hhi)
contsurv$income[contsurv$income == -3105] <- NA

# age
contsurv$age <- as.numeric(contsurv$age)

# collapse education to numeric binary
contsurv$college <- as.numeric(contsurv$education %in% c(4,5,6,7,8))
contsurv$college[contsurv$college == -3105] <- NA

# collapse employed into smaller number of categories and make numeric
contsurv$employed <- as.numeric(contsurv$employment %in% c("Employed full time","Employed part time"))
contsurv$unemployed <- as.numeric(contsurv$employment %in% c("Unemployed looking for work","Unemployed not looking for work"))
contsurv$retstudis <- as.numeric(contsurv$employment %in% c("Retired","Student","Disasbled"))

contsurv$emp_size_comb <- factor(contsurv$emp_size_comb, c("1-5","6-19","20-49","50-199","200-999","1,000-9,999","More than 10,000"))
contsurv$emp_size_comb <- as.numeric(contsurv$emp_size_comb)

# convert empsize into a above/below median dummy
contsurv$emp_size_big <- as.numeric(as.numeric(contsurv$emp_size_comb) > median(as.numeric(contsurv$emp_size_comb), na.rm = TRUE))

# convert used thermometer into above/below median dummies.
contsurv$thrm_corp_pos <- as.numeric(as.numeric(contsurv$thrm_corp) > median(as.numeric(contsurv$thrm_corp), na.rm = TRUE))

## create cleaned version of the data for analysis
contsurv <- contsurv[,c("TradeOpenFavorNum","TradeDealFavorNum",
  "treatment",
  "age","gender","race",
  "college","income","employed","unemployed",
  "party","ideology",
  "thrm_corp","thrm_corp_pos","emp_size_comb","emp_size_big",
  "med_job","med_inq","med_pol"
)]

num_vars <- c("TradeOpenFavorNum","TradeDealFavorNum",
  "age",  
  "college","income","employed","unemployed",
  "party","ideology",
  "thrm_corp","thrm_corp_pos","emp_size_comb","emp_size_big",
  "med_job","med_inq","med_pol")
contsurv[,num_vars] <- apply(contsurv[,num_vars], 2, as.numeric)

##############################
## Average treatment affect ##
##############################

##
contsurvT1relC <- contsurv[contsurv$treatment %in% c("1","3"),]

# 
mod1 <- lm(TradeOpenFavorNum ~ I(treatment=="1"), data = contsurvT1relC);
mod2 <- lm(TradeOpenFavorNum ~ I(treatment=="1") + age + gender + race, data = contsurvT1relC);
mod3 <- lm(TradeOpenFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed, data = contsurvT1relC);
mod4 <- lm(TradeOpenFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = contsurvT1relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC3_expreg_open_T1relC.tex")

# 
mod1 <- lm(TradeDealFavorNum ~ I(treatment=="1"), data = contsurvT1relC);
mod2 <- lm(TradeDealFavorNum ~ I(treatment=="1") + age + gender + race, data = contsurvT1relC);
mod3 <- lm(TradeDealFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed, data = contsurvT1relC);
mod4 <- lm(TradeDealFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = contsurvT1relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC3_expreg_agreement_T1relC.tex")


##
contsurvT2relC <- contsurv[contsurv$treatment %in% c("2","3"),]

# 
mod1 <- lm(TradeOpenFavorNum ~ I(treatment=="2"), data = contsurvT2relC);
mod2 <- lm(TradeOpenFavorNum ~ I(treatment=="2") + age + gender + race, data = contsurvT2relC);
mod3 <- lm(TradeOpenFavorNum ~ I(treatment=="2") + age + gender + race + college + income + employed + unemployed, data = contsurvT2relC);
mod4 <- lm(TradeOpenFavorNum ~ I(treatment=="2") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = contsurvT2relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("T2: Exp. firms ben/Imp-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC3_expreg_open_T2relC.tex")

# 
mod1 <- lm(TradeDealFavorNum ~ I(treatment=="2"), data = contsurvT2relC);
mod2 <- lm(TradeDealFavorNum ~ I(treatment=="2") + age + gender + race, data = contsurvT2relC);
mod3 <- lm(TradeDealFavorNum ~ I(treatment=="2") + age + gender + race + college + income + employed + unemployed, data = contsurvT2relC);
mod4 <- lm(TradeDealFavorNum ~ I(treatment=="2") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = contsurvT2relC);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("T2: Exp. firms ben/Imp-competors harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC3_expreg_agreement_T2relC.tex")

##
contsurvT1relT2 <- contsurv[contsurv$treatment %in% c("1","2"),]

# 
mod1 <- lm(TradeOpenFavorNum ~ I(treatment=="1"), data = contsurvT1relT2);
mod2 <- lm(TradeOpenFavorNum ~ I(treatment=="1") + age + gender + race, data = contsurvT1relT2);
mod3 <- lm(TradeOpenFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed, data = contsurvT1relT2);
mod4 <- lm(TradeOpenFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = contsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC3_expreg_open_T1relT2.tex")

# 
mod1 <- lm(TradeDealFavorNum ~ I(treatment=="1"), data = contsurvT1relT2);
mod2 <- lm(TradeDealFavorNum ~ I(treatment=="1") + age + gender + race, data = contsurvT1relT2);
mod3 <- lm(TradeDealFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed, data = contsurvT1relT2);
mod4 <- lm(TradeDealFavorNum ~ I(treatment=="1") + age + gender + race + college + income + employed + unemployed + 
  party + ideology, data = contsurvT1relT2);

mods <- list(summary(mod1)$coef, summary(mod2)$coef, summary(mod3)$coef, summary(mod4)$coef)
vars <- c(rownames(mods[[4]])[2])
tab <- matrix(data = NA, ncol = length(mods), nrow = length(vars)*2)
rownames(tab) <- c(rep(vars, each = 2))
for(i in 1:length(mods)){mod <- mods[[i]]; tabinmod <- unique(rownames(tab)[rownames(tab) %in% rownames(mod)])
  stars <- rep("", length(tabinmod)); stars[mod[tabinmod,4] < .1] <- "^{}"; stars[mod[tabinmod,4] < .05] <- "^{*}"; 
  stars[mod[tabinmod,4] < .01] <- "^{**}"; stars[mod[tabinmod,4] < .001] <- "^{***}";  
  tab[rownames(tab) %in% rownames(mod),i] <- c(rbind(paste(myround(mod[tabinmod,1],2), stars, sep = ""), paste("(", myround(mod[tabinmod,2],2), ")", sep = "")))}
rownames(tab) <- c(rbind(vars, "")); rownames(tab)[c(1)] <- 
  c("T1: Large firms ben./Small firms harmed") 
N <- c(sum(summary(mod1)$df[1:2]),sum(summary(mod2)$df[1:2]),sum(summary(mod3)$df[1:2]),sum(summary(mod4)$df[1:2]))
N <- paste("\\multicolumn{1}{c}{", N, "}", sep = ""); tab <- rbind(tab, N)
addtorow <- list(); addtorow$pos <- list(2,3); addtorow$command <- c(paste0('\\midrule '),paste0('\\midrule '))
ptable(cbind(rownames(tab), tab), "paper/tables/TableC3_expreg_agreement_T1relT2.tex")


